package com.example.home.volunteer_service.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.home.R;
import com.example.home.volunteer_service.activity.VolunteerListDeatil;
import com.example.home.volunteer_service.bean.VolunteerListBean;

import java.util.List;

public class VolunteerListAdapter extends RecyclerView.Adapter<VolunteerListAdapter.MyHoldy> {
    private Context context;
    private List<VolunteerListBean.RowsBean> list;
    private VolunCallBack volunCallBack;

    public interface VolunCallBack{
        void CallBack(int position);
    }

    public VolunteerListAdapter(Context context, List<VolunteerListBean.RowsBean> list, VolunCallBack volunCallBack) {
        this.context = context;
        this.list = list;
        this.volunCallBack = volunCallBack;
    }

    @NonNull
    @Override
    public VolunteerListAdapter.MyHoldy onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_volist, null);
        return new VolunteerListAdapter.MyHoldy(view);
    }

    @Override
    public void onBindViewHolder(@NonNull VolunteerListAdapter.MyHoldy holder, int position) {
        VolunteerListBean.RowsBean rowsBean = list.get(position);
        holder.title.setText(rowsBean.getTitle());
        holder.undertaker.setText("承办单位:"+rowsBean.getUndertaker());
        holder.startAt.setText("活动开始时间："+rowsBean.getStartAt());
        holder.requireText.setText("要求:"+rowsBean.getRequireText());
        holder.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, VolunteerListDeatil.class);
                intent.putExtra("voluntId",String.valueOf(rowsBean.getId()));
                context.startActivity(intent);
            }
        });
        holder.apply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (volunCallBack != null) {
                    volunCallBack.CallBack(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class MyHoldy extends RecyclerView.ViewHolder {
        private TextView title;
        private TextView undertaker;
        private Button apply;
        private TextView startAt;
        private TextView requireText;
        private RelativeLayout rootView;

        public MyHoldy(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title);
            undertaker = itemView.findViewById(R.id.undertaker);
            apply = itemView.findViewById(R.id.apply);
            startAt = itemView.findViewById(R.id.startAt);
            requireText = itemView.findViewById(R.id.requireText);
            rootView = itemView.findViewById(R.id.rootView);

        }
    }
}
